package com.shop.www.service.Impl;

import com.shop.www.dao.OrderDAO;
import com.shop.www.dao.OrderItemDAO;
import com.shop.www.model.*;
import com.shop.www.service.CartItemservice;
import com.shop.www.service.Commodityservice;
import com.shop.www.service.Orderservice;
import com.shop.www.service.Userservice;

import java.util.List;

public class OrderserviceImpl implements Orderservice {
    private OrderDAO orderDAO;
    //Order和OrderItem共用一个service
    private OrderItemDAO orderItemDAO;
    private CartItemservice cartItemservice;
    private Userservice userservice;
    private Commodityservice commodityservice;


    @Override
    public void addorder(Order order,List<CartItem> cartItemList) {
        //添加订单后把订单ID赋值进去
        orderDAO.addorder(order);
        for(CartItem cartItem :cartItemList){
            //创建订单详情
            OrderItem orderItem = new OrderItem();
            orderItem.setBuycount(cartItem.getBuycount());
            orderItem.setCommodity_ID(cartItem.getCommodity_ID());
            orderItem.setOrder_ID(order.getOrder_ID());
            orderItemDAO.addorderItem(orderItem);
            //修改店铺销量和库存
            Commodity commodity = cartItem.getCommodity();
            Integer sellcount = cartItem.getBuycount();
            commodity.setSaleCount(commodity.getSaleCount()+sellcount);
            commodity.setInventory(commodity.getInventory()-sellcount);
            //如果库存降为0则下架商品
            if(commodity.getInventory()==0){
                commodity.setState("0");
                commodityservice.updatecommoditystate(commodity);
            }
            commodityservice.sellcommodity(commodity);
            //删除购物车中的购物车详情项
            cartItemservice.delCartItem(cartItem);
        }


    }

    @Override
    public List getorderlistByuser(User user) {
        List<Order> orderList = orderDAO.getorderbyuser(user);
        return orderList;
    }

    @Override
    public List getorderItemlist(Integer order_ID) {
        List<OrderItem> orderItemList = orderItemDAO.getorderItem(order_ID);
        for(OrderItem orderItem : orderItemList){
            Commodity commodity = commodityservice.getcommodityByID(orderItem.getCommodity_ID());
            orderItem.setCommodity(commodity);
        }
        return orderItemList;
    }

    @Override
    public List getorderListByseller(Integer user_ID) {
        List<Order> orderList = orderDAO.getorderByseller(user_ID);
        return orderList;
    }

    @Override
    public List getorderListBycourier(Integer user_ID) {
        List<Order> orderList = orderDAO.getorderBycourier(user_ID);
        for(Order order : orderList){
            order.setUser(userservice.getuserByID(order.getUser_ID()));
            order.setSeller(userservice.getuserByID(order.getSeller_ID()));
        }
        return orderList;
    }

    @Override
    public Order getorder(Integer order_ID) {
        return orderDAO.getorderByID(order_ID);
    }

    @Override
    public void updateorder(Order order) {
        orderDAO.updateorder(order);
    }

    @Override
    public List getorderlist(String order_status) {
        List<Order> orderList = orderDAO.getorder(order_status);
        for(Order order : orderList){
            order.setUser(userservice.getuserByID(order.getUser_ID()));
            order.setSeller(userservice.getuserByID(order.getSeller_ID()));
        }
        return orderList;
    }

    @Override
    public void finishorder(Order order) {
        orderDAO.finishorder(order);
    }
}
